/**   
* @Title: PersonServiceImpl.java 
* @Package com.nebula.upms.service.impl 
* @Description: TODO(用一句话描述该文件做什么) 
* @author zhanghui   
* @date 2016年2月18日 上午10:20:09 
* @version V1.0   
*/
package com.nebula.upms.service.biz.impl;

import java.util.Map;

import com.nebula.common.datasource.mybatis.pager.DataPager;
import com.nebula.common.datasource.mybatis.pager.Pager;
import com.nebula.common.util.MD5Util;
import com.nebula.upms.service.biz.PersonService;
import com.nebula.upms.service.model.po.UpmsPersonPO;
import com.nebula.upms.service.model.po.UpmsPersonPOExample;
import com.nebula.upms.service.support.datasource.UpmsDs;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.nebula.common.framework.BaseService;


/** 
* @ClassName: PersonServiceImpl 
* @Description: TODO(这里用一句话描述这个类的作用) 
* @author zhanghui
* @date 2016年2月18日 上午10:20:09 
*  
*/
@Service("personService")
@Transactional
public class PersonServiceImpl extends BaseService implements PersonService {

	/*
	 * <p>Title: getUpmsPersonPOListForPager</p>
	 * <p>Description: </p>
	 * @param params
	 * @param pager
	 * @return
	 * @see com.nebula.upms.service.PersonService#getUpmsPersonPOListForPager(java.util.Map, com.nebula.common.plugs.pager.Pager)
	 */
	@Override
	public DataPager<UpmsPersonPO> getUpmsPersonPOListForPager(Map<String, Object> params, Pager pager) {
		UpmsPersonPOExample example1=new UpmsPersonPOExample();
		UpmsPersonPOExample.Criteria c=example1.createCriteria();
		if(null!=params.get("personName")){
			c.andNameLike("%"+params.get("personName").toString()+"%");
		}
		if(null!=params.get("loginName")){
			c.andLoginNameEqualTo(params.get("loginName").toString());
		}
		DataPager<UpmsPersonPO> dp= UpmsDs.instance().mybatis().ds().mapper().selectDataPager( example1,pager);
		return dp;
	}

	/*
	 * <p>Title: isLoginNameExist</p>
	 * <p>Description: </p>
	 * @param loginName
	 * @return
	 * @see com.nebula.upms.service.PersonService#isLoginNameExist(java.lang.String)
	 */
	@Override
	public boolean isLoginNameExist(String loginName,String oldLoginName) {
		if(loginName.equals(oldLoginName)){
			return false;
		}
		
		UpmsPersonPOExample example1=new UpmsPersonPOExample();
		example1.createCriteria().andLoginNameEqualTo(loginName).andIsDeleteEqualTo(false);
		int count=(int)UpmsDs.instance().mybatis().ds().mapper().countByExample(example1);
		if(count>0){
			return true;
		}
		return false;
	}

	/*
	 * <p>Title: getUpmsPersonPOByKey</p>
	 * <p>Description: </p>
	 * @param personId
	 * @return
	 * @see com.nebula.upms.service.PersonService#getUpmsPersonPOByKey(java.lang.String)
	 */
	@Override
	public UpmsPersonPO getUpmsPersonPOByKey(String personId) {
		return UpmsDs.instance().mybatis().ds().mapper().selectByPrimaryKey(UpmsPersonPO.class, personId);
	}

	/**
	* Description:
	* @param personId
	* @param password
	* @return
	**/
	@Override
	public boolean isPassword(String personId, String password) {
		UpmsPersonPO upmsPersonPO=UpmsDs.instance().mybatis().ds().mapper().selectByPrimaryKey(UpmsPersonPO.class, personId);
		if(upmsPersonPO.getLoginPassword().equals(MD5Util.MD5(password))){
			return true;
		}
		return false;
	}

	/**
	* Description:
	* @param upmsPersonPO
	**/
	@Override
	public void updateUpmsPersonPO(UpmsPersonPO upmsPersonPO) {
		upmsPersonPO.setLoginPassword(MD5Util.MD5(upmsPersonPO.getLoginPassword()));
		UpmsDs.instance().mybatis().ds().mapper().updateByPrimaryKeySelective(upmsPersonPO);
		
	}

}
